package com.googlecode.tabletags.tagsupport;

/**
 * PagerDisplay encapsulates the logic necessary to present the paging
 * information to the pager tag. The tag uses the information to render the
 * display of the cursor detail and the navigation for last, first, next and
 * previous.
 */
public class PagerDisplay {
    private int pageSize;
    private int pageIndex;
    private int totalSize;

    public PagerDisplay() {
    }

    public PagerDisplay(int pageSize, int pageIndex, int totalSize) {
        this.pageSize = pageSize;
        this.pageIndex = pageIndex;
        this.totalSize = totalSize;
    }

    /**
     * Indicates whether or not the pager is empty.
     *
     * @return true if there is no data in the pager.
     */
    public boolean isEmpty() {
        return totalSize == 0;
    }

    /**
     * Returns the total number of data items.
     *
     * @return the total number of items.
     */
    public int getTotalSize() {
        return totalSize;
    }

    /**
     * The current page index. All indexes start at 1.
     *
     * @return the current page index.
     */
    public int getPageIndex() {
        return pageIndex;
    }

    /**
     * The number of items that are displayed per page.
     *
     * @return the number of items displayed per page.
     */
    public int getPageSize() {
        return pageSize;
    }
    
    /**
     * The total number of pages.
     * 
     * @return the total number of pages
     */
    public int getTotalPages() {
        if(totalSize == 0) {
            return 0;
        }
        return ((totalSize - 1) / pageSize) + 1;
    }
    
    /**
     * @return true if there is a next page.
     */
    public boolean hasNextPage() {
        if(totalSize > 0 && pageIndex < getTotalPages()) {
            return true;
        }
        return false;
    }
    
    /**
     * @return true if there is a previous page.
     */
    public boolean hasPreviousPage() {
        if(pageIndex > 1) {
            return true;
        }
        return false;
    }
}
